﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediSIIM.BizComponent.HangZhouSYBInterface;

namespace MediSIIM.BizComponent.HangZhouSYB
{
    public class HangZhouSYBTradeParms
    {
        public string TradeNoIn;
        public string TradeNoOut;
        //交易号
        public string jiaoyihao;
        //交易流水号
        public string ywzqh;
        //
        public string sInData2;
        //
        public string ybyydm;
        //
        public string yylsh;
        //
        public int itemnumber;
        public string CAOZUOYDM;
        //
        /// <summary>
        /// 医保流水号|医保中心返回
        /// </summary>
        public string yblsh;

        public int outbuf;
        public string responsecode;
        //分院代码
        public string fydm;
        public string OutData;
        private string InData;
        private bool Succeed;

        public HangZhouSYBTradeParms()
        { 
        
        }

        public HangZhouSYBTradeParms(TradeType nTradeType, string ywzqh, string sInData, string sInData2, string ybyydm, string yylsh, string fydm, string CAOZUOYDM)
        {
            jiaoyihao = ((int)nTradeType.TradeTypeNo).ToString().PadLeft(6, '0');
            TradeNoIn = MappingInfo.TradeType[nTradeType].Key;
            TradeNoOut = MappingInfo.TradeType[nTradeType].Value;
            this.InData = sInData;
            if (string.IsNullOrEmpty(ywzqh))
            {
               this.ywzqh = string.Empty.PadRight(29);
            }
            else
            {
                this.ywzqh = ywzqh;
            }
            if (string.IsNullOrEmpty(sInData2))
            {
                this.sInData2 = "|";
            }
            else
            {
                this.sInData2 = sInData2;
            }
            if (string.IsNullOrEmpty(CAOZUOYDM))
            {
                CAOZUOYDM = "DBA";
            }
            else
            {
                this.CAOZUOYDM = CAOZUOYDM;
            }
            this.fydm = fydm;
            this.itemnumber = 0;
            this.yylsh = yylsh;
            this.ybyydm = ybyydm;
            this.yblsh = string.Empty.PadRight(21);
            this.outbuf = 1024;
            this.OutData = string.Empty.PadRight(1024);
            this.responsecode = string.Empty.PadRight(2);
        }

        public HangZhouSYBTradeParms YBCall(HangZhouSYBTradeParms parms)
        {
            if (parms == null)
            {
                var vparms = new HangZhouSYBTradeParms(new TradeType() { TradeTypeNo = EnumTradeTypeNo.NULL, MenzhenZhuyuan = MENZHENZHUYUAN.GONGYONG }, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, " ");
                return vparms;
            }
            var retcode = YiBaoJKDLL.Call(
               ref parms.TradeNoIn,
               parms.jiaoyihao,
               parms.ybyydm + "|" + parms.fydm + "|",
               parms.CAOZUOYDM,
               parms.ywzqh,
               parms.ybyydm.Substring(0, 1),
               parms.yylsh,
               parms.InData,
               parms.sInData2,
               parms.itemnumber,
               ref parms.yblsh,
               ref parms.OutData,
               ref parms.outbuf,
               ref parms.responsecode
               );
            if (parms.itemnumber == 1 || parms.itemnumber == 2)
            {
                parms.Succeed = retcode != -1;
            }
            else
            {
                parms.Succeed = parms.TradeNoIn == parms.TradeNoOut && retcode != -1 && parms.responsecode == "00";
            }
            if (!parms.Succeed)
            {
                if (parms.itemnumber == 0 || parms.itemnumber == 9)
                {
                    switch (parms.responsecode)
                    {
                        case "00":
                            return parms;
                        case "70":
                            throw new Exception("交易失败!无效的包" + parms.OutData);
                        case "71":
                            throw new Exception("交易失败!无效的交易类型" + parms.OutData);
                        case "96":
                            throw new Exception("交易失败!系统错误" + parms.OutData);
                        case "80":
                            throw new Exception("交易失败!系统忙" + parms.OutData);
                        case "60":
                            throw new Exception("交易失败!数据库操作失败" + parms.OutData);
                        case "99":
                            throw new Exception("交易失败!业务处理失败" + parms.OutData);
                        case "98":
                            throw new Exception("交易失败!医院开发商分配输出内存空间太小" + parms.OutData);
                        case "10":
                            //签到交易号931100，responsecode == 10 的时候 表示已经签到
                            if (parms.jiaoyihao != "931100")
                            {
                                throw new Exception("医保中心:" + parms.OutData);
                            }
                            break;

                        default:
                            throw new Exception("医保中心:" + parms.OutData);
                    }
                }
            }
            else
            {
                parms.OutData = parms.OutData.Substring(0, parms.OutData.LastIndexOf('|') + 1);
            }
            return parms;
        }
    }
}
